package com.wondersgroup.fjzlpt.console.controller.rest.yljgxx;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.netflix.discovery.converters.Auto;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.wondersgroup.fjzlpt.console.constant.SystemConstant;
import com.wondersgroup.fjzlpt.console.service.inter.yljgxx.YljgZyjrService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.wondersgroup.fjzlpt.console.service.inter.yljgxx.YljgxxService;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@RestController
@RequestMapping("yljgxx")
public class YljgxxRestController {

	/**医疗机构信息*/
	@Autowired
	private YljgxxService yljgxxService;

	/**医疗机构资源接入*/
	@Autowired
	private YljgZyjrService yljgZyjrService;
	
	/**
	 * 分页查询
	 * 会根据当前用户所在机构的机构类别加以限制
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryPageYljgxx")
	public JSONObject queryPageYljgxx(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();
		/*获取当前用户的卫生机构管理类别*/
		String WSJGLBDM = userInfo.getString("WSJGLBDM");
		if(WSJGLBDM.indexOf(SystemConstant.WSGLJGLBDM) == 0){
			/*如果属于管理机构 则按照行政区划查询*/
			String JGXZQHDM = principal.getUserInfo().getString("JGXZQHDM");
			map.put("JGXZQHDM", JGXZQHDM);
		}
		else{
			/*业务机构则只能查询当前机构*/
			String PTJGID = principal.getUserInfo().getString("PTJGID");
			map.put("PTJGID", PTJGID);
		}
		return yljgxxService.queryPage(map);
	}

	@RequestMapping("/queryPageSummary")
	public JSONObject queryPageSummary(HttpServletRequest request){
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();
		/*获取当前用户的卫生机构管理类别*/
		String WSJGLBDM = userInfo.getString("WSJGLBDM");
		if(WSJGLBDM.indexOf(SystemConstant.WSGLJGLBDM) == 0){
			/*如果属于管理机构 则按照行政区划查询*/
			String JGXZQHDM = principal.getUserInfo().getString("JGXZQHDM");
			map.put("JGXZQHDM", JGXZQHDM);
		}
		return yljgxxService.queryPageSummary(map);
	}

	/**
	 * 保存
	 * @param request
	 * @return
	 */
	@RequestMapping("/save")
	public JSONObject save(@RequestParam(value="uploadFile", required = false) MultipartFile file, HttpServletRequest request)
	{
		Map map = RequestUtils.GetParameterMap(request);
		map.put("JGTP", file);
		return yljgxxService.save(map);
	}

	/**
	 * 查询机构资源接入信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryZyjrxx")
	public JSONArray queryZyjrxx(HttpServletRequest request){
		Map map = RequestUtils.GetParameterMap(request);
		return yljgZyjrService.queryZyjrxx(map);
	}

	/**
	 * 保存接入信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveZyJrxx")
	public JSONObject saveZyJrxx(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return yljgZyjrService.save(map);
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	public JSONObject delete(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		return yljgxxService.delete(map);
	}
	
	/**
	 * 将医疗机构信息导出到Excel
	 * @param request
	 * @return
	 */
	@RequestMapping("/downloadToExcel")
	public void downloadToExcel(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
		JSONObject userInfo = principal.getUserInfo();
		/*获取当前用户的卫生机构管理类别*/
		String WSJGLBDM = userInfo.getString("WSJGLBDM");
		if(WSJGLBDM.indexOf(SystemConstant.WSGLJGLBDM) == 0){
			/*如果属于管理机构 则按照行政区划查询*/
			String JGXZQHDM = principal.getUserInfo().getString("JGXZQHDM");
			map.put("JGXZQHDM", JGXZQHDM);
		}
		else{
			/*业务机构则只能查询当前机构*/
			String PTJGID = principal.getUserInfo().getString("PTJGID");
			map.put("PTJGID", PTJGID);
		}
		yljgxxService.downloadToExcel(map, request, response);
	}
}



























